package com.bsoft.exchange.service;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bsoft.exchange.dao.his.DepartmentMapper;
import com.bsoft.exchange.domain.ChildDepartment;
import com.bsoft.exchange.domain.Department;

@Service
public class DepartmentService {
	@Autowired
	DepartmentMapper departmentMapper;
	
	public List<Department> selectList(String hospitalCode,int outpatientType,String departmentCode,String lastModifyTime){
//		System.out.println("ddddddddddddd");
//		List<Department> department = departmentMapper.selectList(hospitalCode, outpatientType,lastModifyTime);
//		for(int i = 0 ; i < department.size() ; i++) {
//			  String departmentcode;
//			  departmentcode = department.get(i).getParentDepartmentCode();
//			  List<ChildDepartment> childdepartment = departmentMapper.selectListChild(hospitalCode,departmentcode);
//			  department.get(i).setDepartmentList(childdepartment);;
//			}
		
		List<Map<String,Object>> department = departmentMapper.selectListAll(hospitalCode, outpatientType,departmentCode);
		Map<String, Department> map = new HashMap<String,Department>();
		for (Map<String, Object> departments : department) {
			if(!map.containsKey(departments.get("parentDepartmentId").toString())){
				Department item = new Department();
				item.setParentDepartmentId(String.valueOf(departments.get("parentDepartmentId")));
				item.setParentDepartmentName((String)departments.get("parentDepartmentName"));
				item.setDepartmentInformation((String)departments.get("departmentInformation"));
				item.setDepartmentlogo((String)departments.get("departmentlogo"));
				item.setDepartmentList(new ArrayList());
		
				map.put(departments.get("parentDepartmentId").toString(), item);
			}
			Department departmentChild = map.get(String.valueOf(departments.get("parentDepartmentId")));
			ChildDepartment childDepartment = new ChildDepartment();
			childDepartment.setDepartmentId(String.valueOf(departments.get("departmentId") ));
			childDepartment.setDepartmentName((String)departments.get("departmentName") );
			childDepartment.setTelephone((String)departments.get("telephone") );
			childDepartment.setAddress((String)departments.get("address") );
			childDepartment.setKeyWord((String)departments.get("keyWord") );
			departmentChild.getDepartmentList().add(childDepartment);

		}
		 List<Department> list =  new  ArrayList();
			for (Entry<String, Department> entry : map.entrySet()) {
				list.add(entry.getValue());
			}
		
		return list;
		
	}

}
